/*
 *   A task scheduler that implements Monte-Carlo simulation
 *
 *   Copyright (C) 2011 Systasis Computer Systems, Inc.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   $Id: HelpPresenter.java 79 2011-08-05 01:13:28Z jchimene@gmail.com $
 */
package com.systasis.sked.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;
import com.systasis.sked.client.resources.SkedConstants;

@Singleton
public class HelpPresenter extends Presenter<HelpPresenter.MyView, HelpPresenter.MyProxy> implements MenuProvider {

    @ProxyCodeSplit
    @NameToken("help")
    public interface MyProxy extends ProxyPlace<HelpPresenter> {
    }

    public interface MyView extends View {

	void helpShow();

	void setContent(String text);
    }

    final private SkedConstants CONSTANTS = GWT.create(SkedConstants.class);

    private boolean helpTextLoaded = false;
    private RequestBuilder requestBuilder;

    @Inject
    public HelpPresenter(EventBus eventBus, MyView view, MyProxy proxy) {
	super(eventBus, view, proxy);
    }

    @Override
    public void callback() {
	try {
	    if (helpTextLoaded) {
		getView().helpShow();
	    } else {
		requestBuilder = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + CONSTANTS.urlHelp());
		requestBuilder.setCallback(new RequestCallback() {

		    @Override
		    public void onError(Request request, Throwable exception) {
			try {
			    throw new Exception(exception);
			} catch (final Exception e) {
			    e.printStackTrace();
			}
		    }

		    @Override
		    public void onResponseReceived(Request request, Response response) {
			try {
			    getView().setContent(response.getText());
			    getView().helpShow();
			    helpTextLoaded = true;
			} catch (final Exception e) {
			    e.printStackTrace();
			}
		    }
		});
		requestBuilder.send();
	    }
	} catch (final Exception e) {
	    e.printStackTrace();
	}

    }

    @Override
    protected void revealInParent() {
	RevealRootContentEvent.fire(this, this);
    }

}
